package com.example.prodect.controller;

import com.example.prodect.dataObject.ProductCategory;
import com.example.prodect.dataObject.ProductInfo;
import com.example.prodect.dto.CartDTO;
import com.example.prodect.service.ProductCategoryService;
import com.example.prodect.service.ProductService;
import com.example.prodect.util.Console;
import com.example.prodect.util.RequestBeanUtil;
import com.example.prodect.vo.ProductInfoVo;
import com.example.prodect.vo.RequestBean;
import com.example.prodect.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductService productService;
    /**
     * 1.查询所有在架的商品
     * 2.获取类目type列表
     * 3.查询类目
     * 4.构造数据
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultVo list(HttpServletRequest request){

        Cookie[] cookies = request.getCookies();
        //查询所有上架商品
        List<ProductInfo> upList = productService.findUpList();
         //查询对应类目
        List<ProductCategory> byCategoryTypeIn = productCategoryService.findByCategoryTypeIn(Arrays.asList(11, 22));

        List<RequestBean> list = new ArrayList<>();
        for (ProductCategory productCategory:byCategoryTypeIn ) {
            RequestBean productVo = new RequestBean();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVo> foods = new ArrayList<>();
            for (ProductInfo productInfo:upList ) {
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVo vo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,vo);
                    foods.add(vo);
                }
            }
            productVo.setFoods(foods);
            list.add(productVo);
        }
        return RequestBeanUtil.success(list);
    }


    /**
     *获取商品列表（给订单服务用）
     * @return
     */
    @PostMapping("/listForOrder")
    @ResponseBody
    public List<ProductInfo> listForOrder(@RequestBody List<String> list){
        List<ProductInfo> in = productService.findByProductIdIn(list);
        Console.log(in.size());
        return  in;
    }


    /**
     * 去库存
     * @param list
     */
    @RequestMapping(value = "/decreaseStock",method = RequestMethod.POST)
    @ResponseBody
    public void decreaseStock(@RequestBody List<CartDTO> list){
        productService.decreaseStock(list);
    }

}
